package cn.test.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 延迟队列
 *
 * @author summit
 * @since 2020/12/1 15:06
 */
@RestController
@RequestMapping("/cache")
@RequiredArgsConstructor
public class CacheController {

    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("/sort")
    public void put() {
        for (int i = 0; i < 10; i++) {
            stringRedisTemplate.opsForZSet().add("Zset", "v" + i, i);
        }
    }

    @GetMapping("/get")
    public void get() {
        Set<ZSetOperations.TypedTuple<String>> zset = stringRedisTemplate.opsForZSet()
            .rangeByScoreWithScores("Zset", 5, 10);
        stringRedisTemplate.opsForZSet().removeRangeByScore("Zset", 5, 10);

        for (ZSetOperations.TypedTuple<String> stringTypedTuple : zset) {
            System.out.println(stringTypedTuple.getValue() + "----" + stringTypedTuple.getScore());
        }
    }

}
